package com.study_mybatis_plus.study_mybatis_plus.com.common.codeGenerator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther lby
 * @Date 2020/9/16
 * 封装代码自动生成工具类
 * 需要导入mybatis-plus,mybatis-plus-generator,
 */
@Slf4j
public class CodeGenerator {

    /**
     * 代码作者
     */
    private  static final String AUTHOR_NAME ="lby";

    /**
     * 数据库地址
     * "jdbc:mysql://47.112.237.64:3306/smart-status?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&autoReconnect=true"
     */
    private static final String DATABASE_URL ="jdbc:mysql://47.112.237.64:3306/smart-status?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&autoReconnect=true";

    /**
     * 数据库用户名
     */
    private static final String DATABASE_USER_NAME ="root";

    /**
     * 数据库密码
     */
    private static final String DATABASE_PASSWORD ="lbyuan@989017";

    /**
     * 父模块名称
     */
    private static  final String PARENT_MODEL_NAME ="com.smartStatus";


    /**
     * 子模块名称
     */
    private static final String MODEL_NAME ="admin";

    /**
     * 表名称
     */
    private static final String TABLE_NAME ="user_student_status";

    public static void codeGen() {
        AutoGenerator generator = new AutoGenerator();
        // 选择 freemarker 引擎，默认 Veloctiy
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setActiveRecord(true);
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(AUTHOR_NAME );
        gc.setOpen(false);
//        取消覆盖
        gc.setFileOverride(false);
//        取消文件名称前缀
        gc.setServiceName("%sService");
        gc.setSwagger2(true);
        gc.setIdType(IdType.AUTO);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        generator.setGlobalConfig(gc);
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(DATABASE_URL );
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername(DATABASE_USER_NAME );
        dataSourceConfig.setPassword(DATABASE_PASSWORD );
        dataSourceConfig.setDbType(DbType.MYSQL);
        generator.setDataSource(dataSourceConfig);
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(MODEL_NAME );
        pc.setParent(PARENT_MODEL_NAME );
        pc.setEntity("api.entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        generator.setPackageInfo(pc);
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(TABLE_NAME );
        strategy.setEntityLombokModel(true);
        strategy.setLogicDeleteFieldName("del_flag");
        strategy.setRestControllerStyle(true);
        generator.setStrategy(strategy);
        /*配置自定义模板*/
        TemplateConfig templateConfig = new TemplateConfig();
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
         templateConfig.setEntity("entryTemplate");
        // templateConfig.setService();
        templateConfig.setController("controllerTemplate");
        templateConfig.setXml("xmlTemplate");
        templateConfig.setMapper("mapperTemplate");
        generator.setTemplate(templateConfig);
        try {
            generator.execute();
            log.info("代码生成成功");
        } catch (Exception e) {
            log.info("代码生成失败");
            e.printStackTrace();
        }
    }

}
